package cn.iocoder.yudao.module.chat.service.gpt;

import cn.iocoder.yudao.module.chat.controller.app.gpt.vo.ChatMessageVO;
import cn.iocoder.yudao.module.chat.dal.dataobject.history.gpt.ChatHistoryContentDO;
import cn.iocoder.yudao.module.chat.dal.dataobject.history.gpt.ChatHistoryDO;
import cn.iocoder.yudao.module.chat.dal.mysql.history.gpt.GptChatHistoryContentMapper;
import cn.iocoder.yudao.module.chat.dal.mysql.history.gpt.GptChatHistoryMapper;
import cn.iocoder.yudao.module.chat.dal.redis.history.gpt.ChatHistoryRedisDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ChatHistoryServiceImpl implements ChatHistoryService{

    @Resource
    private ChatHistoryRedisDao chatHistoryRedisDao;

    @Resource
    private GptChatHistoryMapper gptChatHistoryMapper;
    @Resource
    private GptChatHistoryContentMapper gptChatHistoryContentMapper;

    @Override
    public List<ChatMessageVO> searchRedisHistory(ChatMessageVO chatMessageVO) {
        return chatHistoryRedisDao.searchHistory(chatMessageVO);
    }

    @Override
    public void insertRedisHistory(ChatMessageVO chatMessageVO) {
        chatHistoryRedisDao.insertHistory(chatMessageVO);
    }

    @Override
    @Transactional
    public Long createChatHistory(ChatMessageVO chatMessageVO) {
        ChatHistoryDO gptChatHistoryDO = new ChatHistoryDO();
        gptChatHistoryDO.setUserId(chatMessageVO.getUserId());
        gptChatHistoryDO.setSessionId(chatMessageVO.getSessionId());
        gptChatHistoryMapper.insert(gptChatHistoryDO);
        ChatHistoryContentDO gptChatHistoryContentDO = new ChatHistoryContentDO();
        gptChatHistoryContentDO.setContent(chatMessageVO.getContent());
        gptChatHistoryContentDO.setUserRole(chatMessageVO.getUserRole());
        gptChatHistoryContentDO.setChatHistoryId(gptChatHistoryDO.getId());
        gptChatHistoryContentMapper.insert(gptChatHistoryContentDO);
        return null;
    }
}
